<?php
include("./Core/Common.php");

//清空仿盗版检测数据
if($date==date("Y-m-d 00:00:00") or $date==date("Y-m-d 00:00:01")){
	$DB->exec("DELETE FROM `pay_cron_url`");
}

//检测登陆码登陆状态 180秒后废除
$is_date = date( "Y-m-d H:i:s",(time()-180));
$create_status = time();
$rs=$DB->query("SELECT * FROM `pay_login_qr` WHERE `addtime`>'{$is_date}' and create_status<'{$create_status}' and `cookie`='0' and `qr_url`!='' and type!='wxpay' order by rand() limit 10");
while($res = $rs->fetch())
{
	$create_status = time()+5;
	$DB->exec("update `pay_login_qr` set `create_status` = '{$create_status}' WHERE `id`='{$res['id']}' limit 1");
		$INTL_LOGIN_QRCODE = new INTL_LOGIN_QRCODE();//取登陆码协议协议
	if($res['type']=='qqpay'){
		$data	 = $INTL_LOGIN_QRCODE->Check_QQLogin($res['qr_id']);//获取cookie
	}else{
		$data	 = $INTL_LOGIN_QRCODE->Check_ALILogin($res['qr_id']);//获取cookie
	}
	$urlrow=$DB->query("SELECT * FROM pay_check WHERE url='{$res['get_url']}' order by addtime asc limit 1")->fetch();
	if($data['cookie']){
		$DB->exec("update `pay_login_qr` set `cookie` = '{$data['cookie']}',`msg` = '{$data['msg']}' WHERE `id`='{$res['id']}' limit 1");
		$data = get_curl('http://'.$res['get_url'].'/Api.php?act=pay_zt','cookie='.urlencode($data['cookie']).'&data_data='.urlencode($data_data).'&authcode='.urlencode($urlrow['authcode']).'&qr_id='.urlencode($id));
	}
}


$act=isset($_GET['act'])?$_GET['act']:null;
if($act=='Get_cron')
{	
	$time=time();//当前时间戳
	$timess=time()+180;//当前时间戳 +10秒(下次执行时间)
	if($date<date("Y-m-d 00:5:s")){
		$row=$DB->query("SELECT * from `pay_check` WHERE status='1' and crontime<'{$time}' order by rand() limit 1")->fetch();
		if($row){
			$rs=$DB->query("SELECT * FROM `pay_check` WHERE status='1' and crontime<'{$time}' order by rand() limit 10");
			while($res = $rs->fetch())
			{
				$DB->exec("update `pay_cron_url` set `crontime`='{$timess}' WHERE `id`='{$res['id']}'");//更新数据
				$url_data[]=array("url"=>'http://'.$res['url'].'/Cron.php?Cron=Cron');
			}
			$result=array("code"=>1,"msg"=>"获取监控地址成功","url_data"=>$url_data);
		}else{
			$result=array("code"=>-1,"msg"=>"获取监控地址失败!","url"=>'暂无监控地址');
		}
	}else{
		$row=$DB->query("SELECT * from `pay_cron_url` WHERE crontime<'{$time}' order by rand() limit 1")->fetch();
		if($row){
			$rs=$DB->query("SELECT * FROM `pay_cron_url` WHERE crontime<'{$time}' order by rand() limit 10");
			while($res = $rs->fetch())
			{
				$DB->exec("update `pay_cron_url` set `crontime`='{$timess}' WHERE `id`='{$res['id']}'");//更新数据
				$url_data[]=array("url"=>'http://'.$res['url'].'/Cron.php?Cron=Cron');
			}
			$result=array("code"=>1,"msg"=>"获取监控地址成功","url_data"=>$url_data);
		}else{
			$result=array("code"=>-1,"msg"=>"获取监控地址失败!","url"=>'暂无监控地址');
		}
	}
}
elseif($act='Get_Notify'){
	$time = time();
	$limit = 3;//每次运行执行订单数
	//开始回调金额
	$rs=$DB->query("SELECT * from pay_notify WHERE status='0' and crontime<='{$time}' order by rand() limit {$limit}");
	while($row = $rs->fetch())
	{	
		$data = Get_Notify($row);//向各大前台发送回调
	}
}else{
	$result=array("code"=>-5,"msg"=>"No Act!");
}
if($result)
	exit(json_encode($result));
else
	exit($data);
?>